<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Zookeeper 数据同步与一致性机制 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=NotoSans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
            background-color: #f9f9f9;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
        }
        .highlight {
            position: relative;
            z-index: 1;
        }
        .highlight:after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 40%;
            background-color: rgba(79, 70, 229, 0.2);
            z-index: -1;
            transform: skewY(-2deg);
        }
        .mermaid {
            background-color: white;
            border-radius: 0.5rem;
            padding: 2rem;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        .step-number {
            display: inline-block;
            width: 2rem;
            height: 2rem;
            background-color: #4f46e5;
            color: white;
            border-radius: 50%;
            text-align: center;
            line-height: 2rem;
            margin-right: 1rem;
            font-weight: bold;
        }
        .list-item {
            position: relative;
            padding-left: 2rem;
            margin-bottom: 1rem;
        }
        .list-item:before {
            content: '';
            position: absolute;
            left: 0;
            top: 0.6rem;
            width: 1rem;
            height: 1rem;
            background-color: #4f46e5;
            border-radius: 0.2rem;
            transform: rotate(45deg);
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 md:py-32 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl text-center">
            <h1 class="text-4xl md:text-6xl font-bold mb-6 leading-tight">Zookeeper <span class="highlight">数据同步</span>与一致性机制</h1>
            <p class="text-xl md:text-2xl opacity-90 mb-8 max-w-3xl mx-auto">探索分布式系统中确保数据一致性的核心原理与实现机制</p>
            <div class="flex justify-center space-x-4">
                <a href="#sync" class="bg-white text-indigo-600 hover:bg-indigo-50 px-6 py-3 rounded-lg font-medium transition-all duration-300 transform hover:scale-105">
                    <i class="fas fa-sync-alt mr-2"></i>数据同步
                </a>
                <a href="#inconsistency" class="bg-indigo-700 hover:bg-indigo-800 text-white px-6 py-3 rounded-lg font-medium transition-all duration-300 transform hover:scale-105">
                    <i class="fas fa-exclamation-triangle mr-2"></i>不一致处理
                </a>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 py-12 md:py-16">
        <!-- Intro Section -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-8 md:p-10">
                    <h2 class="text-2xl md:text-3xl font-bold mb-6 text-indigo-700">Zookeeper 一致性概述</h2>
                    <p class="text-lg mb-6">
                        在分布式系统中，<span class="font-semibold text-indigo-600">数据一致性</span>是至关重要的。Zookeeper 作为一个分布式协调服务，实现了强一致性模型，确保所有客户端在任何时刻都能看到一致的数据视图。
                    </p>
                    <div class="grid md:grid-cols-2 gap-8 mt-8">
                        <div class="bg-indigo-50 p-6 rounded-lg border border-indigo-100">
                            <h3 class="text-xl font-semibold mb-4 text-indigo-700 flex items-center">
                                <i class="fas fa-check-circle mr-3"></i> 核心保证
                            </h3>
                            <ul class="space-y-3">
                                <li class="flex items-start">
                                    <i class="fas fa-check text-indigo-500 mt-1 mr-2"></i>
                                    <span>顺序一致性 - 客户端请求按顺序执行</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check text-indigo-500 mt-1 mr-2"></i>
                                    <span>原子性 - 更新操作要么全部成功要么全部失败</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check text-indigo-500 mt-1 mr-2"></i>
                                    <span>单一系统映像 - 客户端看到一致的数据视图</span>
                                </li>
                            </ul>
                        </div>
                        <div class="bg-indigo-50 p-6 rounded-lg border border-indigo-100">
                            <h3 class="text-xl font-semibold mb-4 text-indigo-700 flex items-center">
                                <i class="fas fa-shield-alt mr-3"></i> 设计目标
                            </h3>
                            <ul class="space-y-3">
                                <li class="flex items-start">
                                    <i class="fas fa-bullseye text-indigo-500 mt-1 mr-2"></i>
                                    <span>高可用性 - 即使部分节点故障也能继续服务</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-bolt text-indigo-500 mt-1 mr-2"></i>
                                    <span>高性能 - 读写操作的高吞吐量和低延迟</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-lock text-indigo-500 mt-1 mr-2"></i>
                                    <span>可靠性 - 数据持久化和一致性保证</span>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Data Synchronization Section -->
        <section id="sync" class="mb-20">
            <h2 class="text-3xl font-bold mb-8 text-indigo-700 flex items-center">
                <i class="fas fa-sync-alt mr-4"></i> 节点间的数据同步机制
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="bg-white p-8 rounded-xl shadow-md card-hover">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-700">Zookeeper 的一致性模型</h3>
                    <p class="mb-6">Zookeeper 实现了一个强一致性模型，即在任何时刻，所有客户端都能看到一致的数据视图。这种一致性模型由 Zookeeper 的协议和机制保证。</p>
                    
                    <div class="space-y-4">
                        <div class="flex items-start">
                            <div class="step-number">1</div>
                            <div>
                                <h4 class="font-semibold">Leader 和 Follower 角色</h4>
                                <p class="text-gray-600">Zookeeper 集群中有一个 Leader 节点和多个 Follower 节点。所有的写操作都必须经过 Leader 节点，并由 Leader 节点同步到所有 Follower 节点。</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="step-number">2</div>
                            <div>
                                <h4 class="font-semibold">原子广播协议</h4>
                                <p class="text-gray-600">Zookeeper 使用原子广播协议来确保所有节点的数据同步。Leader 节点负责将写操作的变更广播到所有 Follower 节点。</p>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white p-8 rounded-xl shadow-md card-hover">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-700">Zookeeper 的事务日志</h3>
                    <p class="mb-6">每个 Zookeeper 节点都维护一个事务日志，记录所有的写操作。事务日志用于在节点重新启动或恢复时重建数据状态。</p>
                    
                    <div class="bg-indigo-50 p-4 rounded-lg border border-indigo-100">
                        <h4 class="font-semibold text-indigo-700 mb-2">事务日志关键特性</h4>
                        <ul class="space-y-2">
                            <li class="list-item">持久化存储所有状态变更</li>
                            <li class="list-item">保证写操作的原子性</li>
                            <li class="list-item">支持快速的故障恢复</li>
                            <li class="list-item">定期滚动生成快照</li>
                        </ul>
                    </div>
                </div>
            </div>
            
            <div class="bg-white p-8 rounded-xl shadow-md mb-12 card-hover">
                <h3 class="text-xl font-semibold mb-6 text-indigo-700">数据同步过程详解</h3>
                
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="text-center">
                        <div class="bg-indigo-100 w-16 h-16 rounded-full flex items-center justify-center mx-auto mb-4">
                            <i class="fas fa-paper-plane text-indigo-600 text-2xl"></i>
                        </div>
                        <h4 class="font-semibold mb-2">请求处理</h4>
                        <p class="text-gray-600 text-sm">客户端发起写请求时，Leader 节点处理请求并将变更应用到本地数据存储。</p>
                    </div>
                    <div class="text-center">
                        <div class="bg-indigo-100 w-16 h-16 rounded-full flex items-center justify-center mx-auto mb-4">
                            <i class="fas fa-broadcast-tower text-indigo-600 text-2xl"></i>
                        </div>
                        <h4 class="font-semibold mb-2">变更传播</h4>
                        <p class="text-gray-600 text-sm">Follower 节点接收到变更后，将变更应用到本地数据存储，并记录到事务日志中。</p>
                    </div>
                    <div class="text-center">
                        <div class="bg-indigo-100 w-16 h-16 rounded-full flex items-center justify-center mx-auto mb-4">
                            <i class="fas fa-check-circle text-indigo-600 text-2xl"></i>
                        </div>
                        <h4 class="font-semibold mb-2">变更确认</h4>
                        <p class="text-gray-600 text-sm">Leader 收到大多数节点的确认后返回操作成功响应，确保强一致性。</p>
                    </div>
                </div>
                
                <div class="mt-10">
                    <div class="mermaid">
                        sequenceDiagram
                            participant C as Client
                            participant L as Leader
                            participant F1 as Follower1
                            participant F2 as Follower2
                            participant F3 as Follower3
                            
                            C->>L: 写请求
                            Note right of L: Leader记录事务日志
                            L->>F1: 广播变更
                            L->>F2: 广播变更
                            L->>F3: 广播变更
                            F1-->>L: 确认变更
                            F2-->>L: 确认变更
                            Note right of L: 收到多数确认
                            L-->>C: 操作成功
                    </div>
                </div>
            </div>
        </section>

        <!-- Inconsistency Handling Section -->
        <section id="inconsistency" class="mb-20">
            <h2 class="text-3xl font-bold mb-8 text-indigo-700 flex items-center">
                <i class="fas fa-exclamation-triangle mr-4"></i> 节点数据不一致时的处理
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="bg-white p-8 rounded-xl shadow-md card-hover">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-700">故障恢复机制</h3>
                    
                    <div class="space-y-6">
                        <div>
                            <h4 class="font-semibold mb-2 flex items-center">
                                <i class="fas fa-redo text-indigo-500 mr-2"></i> 节点重启恢复
                            </h4>
                            <p class="text-gray-600">当节点发生故障或重启时，它会从 Leader 节点获取最新的事务日志并恢复数据状态。Leader 节点负责将所有变更广播给重启的节点，以确保它们的数据与集群中的其他节点一致。</p>
                        </div>
                        
                        <div>
                            <h4 class="font-semibold mb-2 flex items-center">
                                <i class="fas fa-camera text-indigo-500 mr-2"></i> 快照恢复
                            </h4>
                            <p class="text-gray-600">除了事务日志，Zookeeper 还定期保存数据的快照。重启时，节点可以使用快照和事务日志来恢复到一致的状态。这种机制可以提高恢复速度和效率。</p>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white p-8 rounded-xl shadow-md card-hover">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-700">选举与健康检查</h3>
                    
                    <div class="space-y-6">
                        <div>
                            <h4 class="font-semibold mb-2 flex items-center">
                                <i class="fas fa-crown text-indigo-500 mr-2"></i> Leader 选举
                            </h4>
                            <p class="text-gray-600">当 Leader 节点失败时，Zookeeper 会进行 Leader 选举，以选择一个新的 Leader 节点。选举过程确保新的 Leader 节点具有最新的数据，并能继续处理客户端请求。</p>
                        </div>
                        
                        <div>
                            <h4 class="font-semibold mb-2 flex items-center">
                                <i class="fas fa-heartbeat text-indigo-500 mr-2"></i> 集群健康检查
                            </h4>
                            <p class="text-gray-600">Zookeeper 集群使用心跳机制和健康检查来检测节点的状态。如果节点失去联系或发生故障，集群会自动调整并选举新的 Leader 节点。</p>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="bg-white p-8 rounded-xl shadow-md card-hover">
                <h3 class="text-xl font-semibold mb-6 text-indigo-700">数据一致性保障</h3>
                
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h4 class="font-semibold mb-4 flex items-center">
                            <i class="fas fa-hashtag text-indigo-500 mr-2"></i> 版本号和序列号
                        </h4>
                        <p class="mb-4 text-gray-600">Zookeeper 节点中的每个数据节点都有一个版本号和序列号。通过这些信息，节点可以检测数据的版本是否一致，并解决潜在的数据冲突。</p>
                        
                        <div class="bg-gray-50 p-4 rounded-lg">
                            <pre class="text-sm text-gray-800">
znode 元数据结构:
{
  data: ...,          // 节点数据
  acl: [...],         // 访问控制列表
  stat: {
    czxid: ...,       // 创建事务ID
    mzxid: ...,       // 修改事务ID
    ctime: ...,       // 创建时间
    mtime: ...,       // 修改时间
    version: ...,     // 数据版本号
    cversion: ...,    // 子节点版本号
    aversion: ...,    // ACL版本号
    ephemeralOwner:...// 临时节点所有者
    dataLength: ...,  // 数据长度
    numChildren: ...  // 子节点数量
  }
}</pre>
                        </div>
                    </div>
                    
                    <div>
                        <h4 class="font-semibold mb-4 flex items-center">
                            <i class="fas fa-bell text-indigo-500 mr-2"></i> Watch 机制
                        </h4>
                        <p class="text-gray-600 mb-6">Zookeeper 使用 Watch 机制来通知客户端数据的变化。当节点的数据发生变化时，Zookeeper 会通知客户端，以确保客户端获取到最新的数据视图。</p>
                        
                        <div class="mermaid">
                            sequenceDiagram
                                participant C as Client
                                participant S as Server
                                
                                C->>S: 读取数据并设置Watch
                                S-->>C: 返回数据
                                Note right of S: 数据变更事件发生
                                S->>C: 发送Watch通知
                                C->>S: 获取最新数据
                                S-->>C: 返回更新后的数据
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Summary Section -->
        <section class="mb-16">
            <div class="bg-indigo-700 text-white rounded-xl overflow-hidden">
                <div class="p-8 md:p-12">
                    <div class="flex flex-col md:flex-row items-center">
                        <div class="md:w-2/3 mb-8 md:mb-0">
                            <h2 class="text-2xl md:text-3xl font-bold mb-4">Zookeeper 一致性核心总结</h2>
                            <p class="text-indigo-100 mb-6">通过精心设计的协议和机制，Zookeeper 在分布式环境中实现了高效、可靠的数据同步与一致性保障。</p>
                            <div class="grid grid-cols-2 gap-4">
                                <div class="flex items-center">
                                    <i class="fas fa-check-circle text-indigo-300 mr-2"></i>
                                    <span>强一致性模型</span>
                                </div>
                                <div class="flex items-center">
                                    <i class="fas fa-check-circle text-indigo-300 mr-2"></i>
                                    <span>原子广播协议</span>
                                </div>
                                <div class="flex items-center">
                                    <i class="fas fa-check-circle text-indigo-300 mr-2"></i>
                                    <span>事务日志与快照</span>
                                </div>
                                <div class="flex items-center">
                                    <i class="fas fa-check-circle text-indigo-300 mr-2"></i>
                                    <span>自动故障恢复</span>
                                </div>
                            </div>
                        </div>
                        <div class="md:w-1/3 flex justify-center">
                            <div class="bg-white text-indigo-700 p-4 rounded-lg shadow-lg w-48 h-48 flex flex-col items-center justify-center">
                                <i class="fas fa-shield-alt text-5xl mb-4"></i>
                                <div class="text-center">
                                    <div class="font-bold text-xl">可靠性</div>
                                    <div class="text-sm">保证</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-semibold text-white mb-2">技术小馆</h3>
                    <p class="text-sm">探索技术奥秘，分享知识结晶</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-indigo-300 hover:text-white transition-colors duration-300 flex items-center">
                        <i class="fas fa-external-link-alt mr-2"></i> 访问技术小馆
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-6 pt-6 text-sm text-center text-gray-500">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // 平滑滚动
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>